package executor;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author tongchen
 * @create 2023-02-11 20:01
 */
public class TestCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        //创建countdownlatch对象
        CountDownLatch countDownLatch = new CountDownLatch(10);
        //创建十个线程
        for (int i = 0; i <10 ; i++) {
            int j=i;
             Thread t1 =new Thread(()->{
                 System.out.println(j+"号选手准备出发");
                 System.out.println(j+"号选手出发");
                 try {
                     TimeUnit.SECONDS.sleep(2);
                     System.out.println(j+"号选手到达终点了");
                     countDownLatch.countDown();
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 }
             });
             t1.start();
        }
        countDownLatch.await();
        System.out.println("比赛结束");
    }
}
